import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jd_flutter/keepAlive/keep_alive_wrapper.dart';
import 'package:jd_flutter/pages/cart.dart';
import 'package:jd_flutter/pages/category.dart';
import 'package:jd_flutter/pages/home.dart';
import 'package:jd_flutter/pages/user.dart';

class TabPage extends StatefulWidget {
  const TabPage({super.key});

  @override
  State<TabPage> createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {

  int _currentIndex = 3;

  late PageController _pageController;


  @override
  void initState() {
    _pageController = PageController(initialPage: _currentIndex);
  }

  final List<Widget> _pageList = const [
    HomePage(),
    CategoryPage(),
    CartPage(),
    UserPage(),
  ];


  PreferredSizeWidget _appbarWidget(){
    return _currentIndex != 3  ? AppBar(
      backgroundColor: Theme.of(context).colorScheme.primary,
      title: InkWell(
        child: Container(
          height: 72.h,
          padding: EdgeInsets.only(left:10.w),
          decoration: BoxDecoration(
              color: Colors.black12,
              borderRadius: BorderRadius.circular(20)
          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Icon(Icons.search),
              Text("笔记本",style: TextStyle(fontSize: 30.sp))
            ],
          ),
        ),
        onTap: (){
          Get.toNamed("/search");
        },
      ),
      leading: const IconButton(
        icon: Icon(Icons.center_focus_weak,size: 28,color: Colors.black87),
        onPressed: null,
      ),
      actions: const [
        IconButton(
            onPressed: null,
            icon: Icon(Icons.message,size: 28,color: Colors.black87)
        )
      ],
    ): AppBar(
      title: const Text("用户中心"),
    );
  }


  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, designSize: const Size(750, 1334));

    return Scaffold(
      appBar: _appbarWidget(),
      body: PageView(
        controller: _pageController,
        children:_pageList,
        onPageChanged: (index){
          setState(() {
            _currentIndex = index;
          });
        },
        //禁止滑动
        // physics: const NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (index){
          setState(() {
            _currentIndex = index;
            _pageController.jumpToPage(index);
          });
        },
        items: const [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "首页"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.category),
              label: "分类"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_cart),
              label: "购物车"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.people),
              label: "我的"
          ),
        ],
      ),
    );
  }
}
